package com.eastedu.superise.handler.question;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eastedu.common.model.question.Fragment;
import com.eastedu.common.model.question.QuestionItemContent;
import com.eastedu.data.RequestParameter;
import com.eastedu.data.handler.AbstractPageHistoricalDataHandler;
import com.eastedu.repository.question.mapper.QuestionBasicInfoMapper;
import com.eastedu.repository.question.mapper.QuestionItemMapper;
import com.eastedu.repository.question.po.QuestionBasicInfoPO;
import com.eastedu.repository.question.po.QuestionItemPO;
import com.eastedu.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


/**
 * #92046 1.将现有库中所有试题的字号均刷为默认(删除历史字号)，（字体、字色等其他设置不变）
 *
 * @author zhangzhishuai
 */
@Slf4j
@Service
@DS("question")
public class FontSizeHistoricalDataHandler extends AbstractPageHistoricalDataHandler<QuestionBasicInfoPO, RequestParameter> {
    public static final String FONT_SIZE = "FontSize";
    private final QuestionBasicInfoMapper questionBasicInfoMapper;
    private final QuestionItemMapper questionItemMapper;

    /**
     * Instantiates a new Choice question answer parentheses historical data handler.
     *
     * @param questionBasicInfoMapper the question basic info mapper
     * @param questionItemMapper      the question item mapper
     */
    public FontSizeHistoricalDataHandler(QuestionBasicInfoMapper questionBasicInfoMapper, QuestionItemMapper questionItemMapper) {
        super(questionBasicInfoMapper);
        this.questionBasicInfoMapper = questionBasicInfoMapper;
        this.questionItemMapper = questionItemMapper;
    }

    @Override
    protected void callback(QuestionBasicInfoPO questionBasicInfoPo, Map<String, Object> map) {
        List<QuestionItemPO> items = questionItemMapper.selectList(Wrappers.<QuestionItemPO>lambdaQuery().eq(QuestionItemPO::getQuestionId, questionBasicInfoPo.getId()));
        if (CollectionUtils.isEmpty(items)) {
            return;
        }
        if (!StrUtil.nullToDefault(JsonUtil.toJsonString(items), "").contains(FONT_SIZE)) {
            return;
        }
        for (QuestionItemPO item : items) {
            item.setContent(removeFontSize(item.getContent()));
            item.setAnswer(removeFontSize(item.getAnswer()));
            item.setOptions(removeFontSize(item.getOptions()));
            item.setExplanation(removeFontSize(item.getExplanation()));
            updateQuestionItem(item);
        }
        questionBasicInfoPo.setQuestion(JsonUtil.toJsonString(items));
        questionBasicInfoMapper.updateById(questionBasicInfoPo);
    }

    private String removeFontSize(String content) {
        List<QuestionItemContent> contents = JsonUtil.parseList(content, QuestionItemContent.class);
        for (QuestionItemContent questionItemContent : contents) {
            // 删除styles 中包含 以FontSize打头的元素
                questionItemContent.getStyles().removeIf(style -> style.startsWith(FONT_SIZE));
            for (Fragment fragment : questionItemContent.getFragments()) {
                fragment.getStyles().removeIf(style -> style.startsWith(FONT_SIZE));
            }
        }
        return JsonUtil.toJsonString(contents);
    }

    private void updateQuestionItem(QuestionItemPO po) {
        this.questionItemMapper.updateById(po);
    }

    @Override
    protected Wrapper<QuestionBasicInfoPO> getWrapper(Serializable serializable, RequestParameter requestParameter) {
        return Wrappers.<QuestionBasicInfoPO>lambdaQuery().gt(QuestionBasicInfoPO::getId, serializable).orderByAsc(QuestionBasicInfoPO::getId);
    }
}
